package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    //物资类别列表 目前两个地方需要
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        List<ProductCategoryTreeNodeVO> ProductCategoryTreeNodeVOList = productCategoryService.categoryTree("一二三");
        //TODO 在业务层分页
        List<ProductCategoryTreeNodeVO> ProductCategoryTreeNodeVOListPage = ListPageUtils.page(ProductCategoryTreeNodeVOList, pageSize, pageNum);
        return ResponseBean.success(new PageVO<ProductCategoryTreeNodeVO>(ProductCategoryTreeNodeVOList.size(),ProductCategoryTreeNodeVOListPage));
    }

    //添加时获取一二级分类
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> ProductCategoryTreeNodeVOList = productCategoryService.categoryTree("一二");
        return ResponseBean.success(ProductCategoryTreeNodeVOList);
    }

    //添加分类
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO) {
        productCategoryService.add(productCategoryVO);
        return ResponseBean.success();
    }

    //修改分类时回显信息
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        ProductCategory productCategory = productCategoryService.selectProductCategoryById(id);
        return ResponseBean.success(productCategory);
    }

    //修改分类信息
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody Map editRuleForm) {
        productCategoryService.update(id, editRuleForm);
        return ResponseBean.success();
    }

    //删除分类  有子分类的、和物资引用的分类不能删除
    //TODO 有物资引用的分类不能删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        boolean flag = productCategoryService.delete(id);
        if (flag)
            return ResponseBean.success();
        else {
            Map errorMsgMap = new HashMap<>();
            errorMsgMap.put("errorMsg", " 有子分类或物资引用的分类不能删除!!!");
            return ResponseBean.error(errorMsgMap);
        }
    }
}
